package bairui.international.console.web.controller.product.product;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.common.config.Config_Setup;
import bairui.international.model.domain.jpa.QTB_Product;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.service.Service_Product;

/**
 *
 * 物料列表
 *
 * @generated
 * 
 * @see STest_C_Product_Product_Page_Page 测试
 *
 */
@Controller
public class C_Product_Product_Page_Page
 extends SC_Product_Product_Page_Page 
{
	
	@Autowired private Service_Product service_Product;
	
	/**
	 * 分页列表
	 */
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request
	) throws Exception {
		org.springframework.data.domain.Page<TB_Product> page = service_Product.page(
				it->filter(it, request),  
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		page.forEach(it->{
			it.setImgUrl(new C_Product_Product_Show_ShowImg.ShowImg.Operate(){
				@Override public Object getId() {
					return it.getId();
				}
			}.getUrl());
		});
		//
		return Page.Output.output(
				page
			);
	}
	
//	public static void gainProperty(TB_Product product, Service_ProductPropertyItem service_ProductPropertyItem){
//	product.setProductPropertyItems(
//			service_ProductPropertyItem
//				.list(it3->it3.products.any().eq(product))
//				.stream()
//				.map(it3->{
//					TB_ProductPropertyItem item = new TB_ProductPropertyItem();
//						item.setName(it3.getName());
//					return item;
//				})
//				.collect(Collectors.toList())
//		);
//}

	public static Sort sort(QTB_Product it, HttpServletRequest request) {
		return Page.Output.sort(it, request, new Page.Output.OrderExtends() {
			@Override public Sort gainProductCategory(Direction direction) {
				return sort(direction, it.productCategory.name);
			}
			@Override public Sort gainWineBottleNormsWine(Direction direction) {
				return sort(direction, it.wineBottleNormsWine.name);
			}
		});
	}
	
	public static BooleanBuilder filter(QTB_Product it, HttpServletRequest request) {
		return Page.Filter.query(it, request)
				.and(it.rootCategory.name.ne(Config_Setup.Barter))
				.and(it.rootCategory.name.ne(Config_Setup.Advance))
				.and(it.rootCategory.name.ne(Config_Setup.Delicate))
				.and(it.rootCategory.name.ne(Config_Setup.Wrap));
	}

}
